package com.cloudxingkong.dianyinke.Student.Ui;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import android.view.View;

import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Fragment.FuJinLaoShiFragment;
import com.cloudxingkong.dianyinke.Student.Fragment.KeMuFragment;
import com.cloudxingkong.dianyinke.Student.Fragment.PingFenFragment;
import com.cloudxingkong.dianyinke.Student.Utils.NoScrollViewPager;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.flyco.tablayout.SlidingTabLayout;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/*
课程预约 Activity
 */
public class KeChengYuYue extends BaseActivity implements KeMuFragment.GetAdapterPosition {

    private List<Fragment> listFragment;
    private SlidingTabLayout tabLayout;
    private FuJinLaoShiFragment laoShiFragment;
    private PingFenFragment pingFenFragment;
    private FragmentManager fragmentManager;
    private KeMuFragment keMuFragment;
    public NoScrollViewPager viewPager;
    public static final String[] mTitles = {"科目", "附近老师", "评分"};
    private String name;
    private YuYueVPAdapter yuYueVPAdapter;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setContentView(R.layout.activity_ke_cheng_yu_yue);
        // loadData();
        init();

    }


    private void loadData() {
        x.http().get(new RequestParams(ThisUri.SERVER_YUMING + StudentUri.STUDENT_SUBJECT),
                new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Log.e("test", result);

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
    }

    private void init() {
        fragmentManager = getSupportFragmentManager();
        tabLayout = (SlidingTabLayout) findViewById(R.id.kcyy_tablayout);
        viewPager = (NoScrollViewPager) findViewById(R.id.kcyy_viewpager);

        tabLayout.setTabSpaceEqual(true);

        viewPager.setPadingEnadled(false);

        listFragment = new ArrayList<>();

        laoShiFragment = new FuJinLaoShiFragment();
        pingFenFragment = new PingFenFragment();
        keMuFragment = new KeMuFragment();


        listFragment.add(keMuFragment);
        listFragment.add(laoShiFragment);
        listFragment.add(pingFenFragment);


        yuYueVPAdapter = new YuYueVPAdapter(getSupportFragmentManager());
        viewPager.setAdapter(yuYueVPAdapter);

        tabLayout.setViewPager(viewPager, mTitles);
        tabLayout.setTextSelectColor(Color.parseColor("#E576B0"));
        tabLayout.setTextUnselectColor(Color.parseColor("#9f9f9f"));
        tabLayout.setIndicatorColor(Color.parseColor("#E576B0"));


    }


    public void back(View view) {
        finish();
    }

    /**
     * Activity实现接口  此时position为kemufragment中的listview条目中的gridview的条目
     *
     * @param position
     */
    @Override
    public void getPosition(int position, String pname) {
        tabLayout.setTextSelectColor(R.color.main);
        viewPager.setCurrentItem(1, true);
    }


    private class YuYueVPAdapter extends FragmentPagerAdapter {

        public YuYueVPAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return listFragment.get(position);
        }

        @Override
        public int getCount() {
            return mTitles.length;
        }

        //listTitle.get(position % listFragment.size())

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }
}
